﻿using MediatR;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.LineCommand;
using T.Basic.Repository.BasicInterface;

namespace T.Basic.Read.API.Appliction.CommandHandlers.LineHanders
{
    /// <summary>
    /// 显示线路信息
    /// </summary>
    public class LineListCommandHandler : IRequestHandler<LineListCommand, PageDto<Line>>
    {
       private readonly ILineRepository _lineRepository;
        public LineListCommandHandler(ILineRepository lineRepository)
        {
            _lineRepository = lineRepository;
        }

        public async Task<PageDto<Line>> Handle(LineListCommand request, CancellationToken cancellationToken)
        {
            PageDto<Line> pageDto = new PageDto<Line>();
            var linelist = from a in await _lineRepository.GetAllAsync()
                           where !a.IsDeleted
                           select a;
            if (!string.IsNullOrEmpty(request.CompanyID))
            {
                linelist = linelist.Where(d => d.CompanyID == request.CompanyID).ToList();
            }
            //线路名称
            if (!string.IsNullOrEmpty(request.LineName))
            {
                linelist=linelist.Where(d=>d.LineName.Contains(request.LineName)).ToList();
            }
            //起点
            if (!string.IsNullOrEmpty(request.StartingPoint))
            {
                linelist = linelist.Where(d => d.StartingProvince.Contains(request.StartingPoint)|| d.StartingCity.Contains(request.StartingPoint)
                || d.StartingCounty.Contains(request.StartingPoint) || d.StartingPointAddress.Contains(request.StartingPoint)).ToList();
            }
           
            //终点
            if (!string.IsNullOrEmpty(request.Ends))
            {
                linelist = linelist.Where(d => d.EndProvince.Contains(request.Ends) || d.EndCity.Contains(request.Ends)
                || d.EndCounty.Contains(request.Ends) || d.EndAddress.Contains(request.Ends)).ToList();
            }

            //是否是外协

            if (!string.IsNullOrEmpty(request.OutsourcedUnitsIsno))
            {
                bool isnofo = false;
                if (request.OutsourcedUnitsIsno == "是")
                {
                    isnofo = true;
                }

                linelist = linelist.Where(d => d.OutsourcedUnitsIsno == isnofo).ToList();
            }
            //货主手机号
            if (!string.IsNullOrEmpty(request.CargoOwnerPhone))
            {
                linelist = linelist.Where(d => d.CargoOwnerPhone.Contains(request.CargoOwnerPhone)).ToList();
            }
            //货主单位
            if (!string.IsNullOrEmpty(request.CargoOwnerUnitName))
            {
                linelist = linelist.Where(d => d.CargoOwnerUnitName.Contains(request.CargoOwnerUnitName)).ToList();
            }
            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }
            if (request.PageSize == null)
            {
                request.PageSize = linelist.Count();
            }
            var size = request.PageSize;
            var index = request.PageIndex;
            pageDto.TotalCount = linelist.Count();
            pageDto.PageCount = (int)Math.Ceiling(pageDto.PageCount * 1.0 / (int)size);
            pageDto.list = linelist.OrderByDescending(d => d.CreatedDate).Skip(((int)index - 1) * (int)size).Take((int)size).ToList();
            return pageDto;
            //pageDto.TotalCount = linelist.Count();
            //pageDto.PageCount = (int)Math.Ceiling(pageDto.TotalCount*1.0 / request.PageSize);
            //pageDto.list = linelist.OrderByDescending(d => d.CreatedDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            //return pageDto;

        }
    }
}
